package com.myfoon.model.entryprocessor;

import com.tangosol.io.ExternalizableLite;
import com.tangosol.util.InvocableMap;
import com.tangosol.util.processor.AbstractProcessor;

import java.io.DataInput;
import java.io.DataOutput;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

public class EventSubmitter extends AbstractProcessor implements ExternalizableLite {
    private Long val;
    private int max;
    
    public EventSubmitter() {
    }
    
    public EventSubmitter(Long val, int max) {
        this.val = val;
        this.max = max;
    }
    
    public Object process(InvocableMap.Entry entry) {                
        if (entry == null) 
            return null;        
        
        List<Long> inbox = null;
        if (entry.getValue() == null) 
            inbox = new ArrayList<Long>();
        else {
            inbox = (List<Long>)entry.getValue();
        }
        
        
        if (max > 0 && inbox.size() == max) {
            inbox.remove(0);
        }        
        
        inbox.add(val);
        entry.setValue(inbox);
        return null;
    }

    public void setVal(Long val) {
        this.val = val;
    }

    public Long getVal() {
        return val;
    }

    public void readExternal(DataInput dataInput) throws IOException {
        val = dataInput.readLong();
        max = dataInput.readInt();
    }    

    public void writeExternal(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(val);
        dataOutput.writeInt(max);
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getMax() {
        return max;
    }
}
